﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using Coco.Models;
using Coco.Library;
using System.Windows.Input;

namespace Coco.ViewModel
{
    public class TodoViewModel : ViewModelBase
    {
        public TodoViewModel() { }

        #region Properties
        private TodoItemInfo itemToAdd = new TodoItemInfo();
        public TodoItemInfo ItemToAdd
        {
            get { return itemToAdd; }
            set
            {
                itemToAdd = value;
                RaisePropertyChanged(() => ItemToAdd);
                RaisePropertyChanged(() => AddCommand);
            }
        }

        private ObservableCollection<TodoItemInfo> allTodoCollection;
        public ObservableCollection<TodoItemInfo> AllTodoCollections
        {
            get
            {
                return allTodoCollection;
            }
            set
            {
                allTodoCollection = value;
                RaisePropertyChanged(() => AllTodoCollections);
            }
        }

        private TodoItemInfo selectedTodoItem;
        public TodoItemInfo SelectedTodoItem
        {
            get { return selectedTodoItem; }
            set
            {
                selectedTodoItem = value;
                RaisePropertyChanged(() => SelectedTodoItem);
                RaisePropertyChanged(() => RemoveCommand);
            }
        }

        private string searchKeywords = string.Empty;
        public string SearchKeywords
        {
            get { return searchKeywords; }
            set
            {
                searchKeywords = value;
                RaisePropertyChanged(() => SearchKeywords);
            }
        }
        #endregion

        #region Method
        private void Query()
        {
            allTodoCollection = new ObservableCollection<TodoItemInfo>(Database.CollectionOfTodoItems.Where(e => e.Time >= DateTime.Now));
            RaisePropertyChanged(() => AllTodoCollections);
        }

        private void Insert(TodoItemInfo entity)
        {
            Database.CollectionOfTodoItems.InsertOnSubmit(entity);
            SaveChanges();
            Query();
        }

        private void Remove(TodoItemInfo entity)
        {
            Database.CollectionOfTodoItems.DeleteOnSubmit(entity);
            SaveChanges();
            Query();
        }

        private void Update(TodoItemInfo entity)
        {
            var item = Database.CollectionOfTodoItems.FirstOrDefault(e => e.Guid == entity.Guid);
            item = entity;
            SaveChanges();
        }
        #endregion

        #region Commands
        public ICommand AddCommand
        {
            get
            {
                return new GenericCommand()
                {
                    CanExecuteCallback = delegate { return !string.IsNullOrWhiteSpace(ItemToAdd.Title); },
                    ExecuteCallback = delegate { Insert(ItemToAdd); }
                };
            }
        }

        public ICommand RemoveCommand
        {
            get
            {
                return new GenericCommand()
                {
                    CanExecuteCallback = delegate { return SelectedTodoItem != null; },
                    ExecuteCallback = delegate { Remove(SelectedTodoItem); }
                };
            }
        }
        #endregion
    }
}
